package com.seqb.common.task;

import com.seqb.utils.DateUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

/**
 * @author lilinshen
 * @title 世纪KTV签到
 * @description 请填写相关描述
 * @date 2020/5/9
 */
@Component
public class ShiJiKtvJob implements Job {
    private static final Logger LOGGER = LoggerFactory.getLogger(DongdongNongChangJob.class);
    public static final HttpHeaders headers = new HttpHeaders();


    @Autowired
    private RestTemplate restTemplate;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        try {
            LOGGER.info("世纪KTV签到任务开始-----------" + DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            shiJiKtvSignIn();
            LOGGER.info("世纪KTV签到任务结束-----------" + DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 世纪KTV签到
     */
    public boolean shiJiKtvSignIn() {
        try {
            String requestUrl = "http://www.sz8y8.com/ktv.wx/wx/checkin.action?";
            HttpMethod httpMethod = HttpMethod.POST;
            MultiValueMap<String, Object> requestMap = new LinkedMultiValueMap<>();
            requestMap.add("openid", "oUwCVv-WyB_kioOOHkzMQ7vqC3VQ");
            requestMap.add("merid", "10009");
            HttpEntity httpEntity = new HttpEntity(requestMap, headers);
            ResponseEntity<String> responseEntity = restTemplate.exchange(requestUrl, httpMethod, httpEntity, String.class);
            LOGGER.info("世纪KTV签到操作-ydn：" + responseEntity.getBody());

            requestUrl = "http://www.sz8y8.com/ktv.wx/wx/checkin.action?";
            httpMethod = HttpMethod.POST;
            requestMap = new LinkedMultiValueMap<>();
            requestMap.add("openid", "oUwCVvyZ9vULi6uzkGskTPNr4g8g");
            requestMap.add("merid", "10009");
            httpEntity = new HttpEntity(requestMap, headers);
            responseEntity = restTemplate.exchange(requestUrl, httpMethod, httpEntity, String.class);
            LOGGER.info("世纪KTV签到操作-lls：" + responseEntity.getBody());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
